package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.fragments.SelectMyDigestBookFragment;
import me.shurufa.model.AddDigestBean;
import me.shurufa.utils.Constants;

/**
 * 作者 : andy
 * 日期 : 16/3/8 17:50
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 选择书摘本界面
 */
public class SelectDigestBookActivity extends BaseActivity {

    @Bind(R.id.fragment_container)
    FrameLayout fragment_container;

    private AddDigestBean mAddDigestBean;
    private SelectMyDigestBookFragment selectMyDigestBookFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_select_digest_book;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
    }


    public static void initArguments(Intent in, AddDigestBean addDigestBean) {
        in.putExtra(Constants.ARG_ADD_DIGEST, addDigestBean);
    }

    private void parseArguments() {
        mAddDigestBean = (AddDigestBean) getIntent().getSerializableExtra(Constants.ARG_ADD_DIGEST);
    }

    private void initUI() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        selectMyDigestBookFragment = SelectMyDigestBookFragment.create(mAddDigestBean);
        ft.replace(R.id.fragment_container, selectMyDigestBookFragment);
        ft.commit();
    }

    public void onEventMainThread(Intent in) {
        if (Constants.ACTION_BOOKSHELVES_REFRESH.equals(in.getAction())) {
            selectMyDigestBookFragment.refresh();
        }

        if (Constants.EVENT_ADD_NOTEBOOK_FROM_ADD_DIGEST == in.getAction()) {
            selectMyDigestBookFragment.refresh();
        }
    }

}
